using System.Collections.Generic;
using RuntimeInspectorNamespace;
using UnityEngine;

namespace Yoozoo.Gameplay.City
{
    public class DecorationManager
    {
       public Dictionary<long,DecorationRenderData> renderData = new Dictionary<long,DecorationRenderData>();
       
       private DecorationDrawManager drawManager;

       private Camera camera;
       private Transform cameraTrans;

       public void Initialize()
       {
           var root = new GameObject("[DecorationManager]");
           drawManager = root.AddComponent<DecorationDrawManager>();
       }

       public void SetCamera(Camera camera)
       {
           this.camera = camera;
           cameraTrans = camera.transform;
           drawManager.SetCamera(camera);
       }

       public void AddEntity(long index,GameObject prefab)
       {
           var meshData = prefab.GetComponent<DecorationMeshData>();
           if (meshData != null)
           {
               renderData[index] = new DecorationRenderData
               {
                   position = Vector3.zero,
                   scale = Vector3.one,
                   eulerAngle = Vector3.zero,
                   meshData = meshData,
               };
               drawManager.AddEntity(index,meshData);
           }
       }

       public void RemoveEntity(long index)
       {
           if (renderData.TryGetValue(index, out var data))
           {
               drawManager.RemoveEntity(index);
               renderData.Remove(index);
           }
       }

       public void SetPosition(long index,Vector3 position)
       {
           if (renderData.TryGetValue(index, out var data))
           {
               data.position = position;
               drawManager.SetTRS(index,data.position,data.eulerAngle,data.scale);
           }
       }

       public void SetScale(long index,Vector3 scale)
       {
           if (renderData.TryGetValue(index, out var data))
           {
               data.scale = scale;
               drawManager.SetTRS(index,data.position,data.eulerAngle,data.scale);
           }
       }

       public void SetRotation(long index,Vector3 rotation)
       {
           if (renderData.TryGetValue(index, out var data))
           {
               data.eulerAngle = rotation;
               drawManager.SetTRS(index,data.position,data.eulerAngle,data.scale);
           }
       }

       public void Clear()
       {
           renderData.Clear();
           drawManager.Clear();
       }

       public void SetEnabled(bool enabled)
       {
           drawManager.enabled = enabled;
       }

       public void Dispose()
       {
           Clear();
           if (drawManager)
           {
               GameObject.Destroy(drawManager);
               drawManager = null;
           }
       }
    }
}